﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.DataFactory;
using Enterprise.Model.Doc;
using System.Data;

namespace Enterprise.Data.Doc
{
    public class Doc_Class
    {
        public Doc_Class()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(decimal CLASSID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DOC_CLASS");
			strSql.Append(" where CLASSID="+CLASSID+" ");
			return  DbExecute.Exist(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(DocClassEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.CLASSID != null)
			{
				strSql1.Append("CLASSID,");
				strSql2.Append(""+model.CLASSID+",");
			}
			if (model.CLASSNAME != null)
			{
				strSql1.Append("CLASSNAME,");
				strSql2.Append("'"+model.CLASSNAME+"',");
			}
			if (model.CLASSVIEWROLES != null)
			{
				strSql1.Append("CLASSVIEWROLES,");
				strSql2.Append("'"+model.CLASSVIEWROLES+"',");
			}
			if (model.CLASSORDERBY != null)
			{
				strSql1.Append("CLASSORDERBY,");
				strSql2.Append(""+model.CLASSORDERBY+",");
			}
			if (model.CLASSURL != null)
			{
				strSql1.Append("CLASSURL,");
				strSql2.Append("'"+model.CLASSURL+"',");
			}
			if (model.CLASSFLAG != null)
			{
				strSql1.Append("CLASSFLAG,");
				strSql2.Append(""+model.CLASSFLAG+",");
			}
			if (model.CLASSPUBROLES != null)
			{
				strSql1.Append("CLASSPUBROLES,");
				strSql2.Append("'"+model.CLASSPUBROLES+"',");
			}
			strSql.Append("insert into DOC_CLASS(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
            int rows = DbExecute.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DocClassEntity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update DOC_CLASS set ");
			if (model.CLASSNAME != null)
			{
				strSql.Append("CLASSNAME='"+model.CLASSNAME+"',");
			}
			else
			{
				strSql.Append("CLASSNAME= null ,");
			}
			if (model.CLASSVIEWROLES != null)
			{
				strSql.Append("CLASSVIEWROLES='"+model.CLASSVIEWROLES+"',");
			}
			else
			{
				strSql.Append("CLASSVIEWROLES= null ,");
			}
			if (model.CLASSORDERBY != null)
			{
				strSql.Append("CLASSORDERBY="+model.CLASSORDERBY+",");
			}
			else
			{
				strSql.Append("CLASSORDERBY= null ,");
			}
			if (model.CLASSURL != null)
			{
				strSql.Append("CLASSURL='"+model.CLASSURL+"',");
			}
			else
			{
				strSql.Append("CLASSURL= null ,");
			}
			if (model.CLASSFLAG != null)
			{
				strSql.Append("CLASSFLAG="+model.CLASSFLAG+",");
			}
			else
			{
				strSql.Append("CLASSFLAG= null ,");
			}
			if (model.CLASSPUBROLES != null)
			{
				strSql.Append("CLASSPUBROLES='"+model.CLASSPUBROLES+"',");
			}
			else
			{
				strSql.Append("CLASSPUBROLES= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where CLASSID="+ model.CLASSID+" ");
			int rowsAffected=DbExecute.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(decimal CLASSID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DOC_CLASS ");
			strSql.Append(" where CLASSID="+CLASSID+" " );
			int rowsAffected=DbExecute.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string CLASSIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DOC_CLASS ");
			strSql.Append(" where CLASSID in ("+CLASSIDlist + ")  ");
            int rows = DbExecute.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DocClassEntity GetModel(decimal CLASSID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  ");
			strSql.Append(" CLASSID,CLASSNAME,CLASSVIEWROLES,CLASSORDERBY,CLASSURL,CLASSFLAG,CLASSPUBROLES ");
			strSql.Append(" from DOC_CLASS ");
			strSql.Append(" where CLASSID="+CLASSID+" " );
			DocClassEntity model=new DocClassEntity();
            DataSet ds = DbExecute.GetDataSet(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DocClassEntity DataRowToModel(DataRow row)
		{
			DocClassEntity model=new DocClassEntity();
			if (row != null)
			{
				if(row["CLASSID"]!=null && row["CLASSID"].ToString()!="")
				{
					model.CLASSID=decimal.Parse(row["CLASSID"].ToString());
				}
				if(row["CLASSNAME"]!=null)
				{
					model.CLASSNAME=row["CLASSNAME"].ToString();
				}
				if(row["CLASSVIEWROLES"]!=null)
				{
					model.CLASSVIEWROLES=row["CLASSVIEWROLES"].ToString();
				}
				if(row["CLASSORDERBY"]!=null && row["CLASSORDERBY"].ToString()!="")
				{
					model.CLASSORDERBY=decimal.Parse(row["CLASSORDERBY"].ToString());
				}
				if(row["CLASSURL"]!=null)
				{
					model.CLASSURL=row["CLASSURL"].ToString();
				}
				if(row["CLASSFLAG"]!=null && row["CLASSFLAG"].ToString()!="")
				{
					model.CLASSFLAG=decimal.Parse(row["CLASSFLAG"].ToString());
				}
				if(row["CLASSPUBROLES"]!=null)
				{
					model.CLASSPUBROLES=row["CLASSPUBROLES"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select CLASSID,CLASSNAME,CLASSVIEWROLES,CLASSORDERBY,CLASSURL,CLASSFLAG,CLASSPUBROLES ");
			strSql.Append(" FROM DOC_CLASS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return DbExecute.GetDataSet(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM DOC_CLASS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbExecute.ExecuteScalar(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.CLASSID desc");
			}
			strSql.Append(")AS R, T.*  from DOC_CLASS T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.R between {0} and {1}", startIndex, endIndex);
            return DbExecute.GetDataSet(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		
        /// <summary>
        /// 栏目是否有文章
        /// </summary>
        /// <param name="_cid"></param>
        /// <returns></returns>
        public bool HasArticle(decimal CLASSID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from DOC_ARTICLES");
            strSql.Append(" where CLASSID=" + CLASSID + " ");
            return DbExecute.Exist(strSql.ToString());
        }
        
       
        /// <summary>
        /// 允许发布的权限
        /// </summary>
        /// <param name="dt">返回一个栏目的Datatable:classid,classname,qx=1为可以发布</param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public bool AllowPub(out DataTable dt,int userid)
        {
            bool rbool = false;
            dt= DbExecute.GetDataSet("select t.classid, t.classname, ','||t.classpubroles||',' as rolelist ,'0' as qx from doc_class t ").Tables[0];

            System.Data.DataTable dt2 = DbExecute.GetDataSet("select t.roleid from sys_userrole t where userid="+userid).Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataRow dr2 in dt2.Rows)
                {
                    if (dr["rolelist"].ToString().Contains("," + dr2["roleid"].ToString() + ","))
                    {
                        dr["qx"] = 1;
                        rbool = true;
                    }
                }
            }           
            return rbool;
            
        }

        /// <summary>
        /// 检测某个栏目是否有发布权限
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public bool AllowPubInClass(decimal classid, int userid)
        {
            bool rbool = false;

            DataTable dt = new DataTable();
            AllowPub(out dt, userid);

            int t = dt.Select("qx='1' and classid=" + classid).Count();
            if (t > 0)
                rbool = true;
            else
                rbool = false;
            return rbool;
        }

        /// <summary>
        /// 检测某个栏目是否有发布权限
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="userid"></param>
        /// <returns>没有任何权限返回：(0)；有权限返回：(1,21)</returns>
        public string GetUserManageClass(int userid)
        {
            string Ids = "(";

            DataTable dt = new DataTable();
            AllowPub(out dt, userid);
            DataRow[] drs = dt.Select("qx='1'");
            foreach (DataRow dr in drs)
            {
                Ids += dr[0]+",";
            }
            if (Ids.Length > 0)
            {
                Ids = Ids.Remove(Ids.Length - 1);
            }
            else
            {
                Ids = "0";
            }
            Ids += ")";
            return Ids;
        }

        #endregion  MethodEx
    }
}
